﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class Like_TinRaoVatDAO
    {
        public static List<Like_TinRaoVatDTO> getAll()
        {
            List<Like_TinRaoVatDTO> result = new List<Like_TinRaoVatDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LIKE_TINRAOVATs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                Like_TinRaoVatDTO dto = new Like_TinRaoVatDTO();

                dto.MaLike = ds.MaLike;
                dto.MaTinRaoVat = ds.MaTinRaoVat.Value;
                dto.MaNguoiDung = ds.MaNguoiDung.Value;
                dto.ThoiGianLike = ds.ThoiGianLike.Value;
                
                result.Add(dto);
            }

            return result;
        }

        public static Like_TinRaoVatDTO getAlongCode(int maLike)
        {
            Like_TinRaoVatDTO result = new Like_TinRaoVatDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LIKE_TINRAOVATs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaLike == maLike).Single();

            result.MaLike = query.MaLike;
            result.MaTinRaoVat = query.MaTinRaoVat.Value;
            result.MaNguoiDung = query.MaNguoiDung.Value;
            result.ThoiGianLike = query.ThoiGianLike.Value;

            return result;
        }

        public static void Insert(Like_TinRaoVatDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LIKE_TINRAOVAT like = new LIKE_TINRAOVAT();
            like.MaTinRaoVat = _insert.MaTinRaoVat;
            like.MaNguoiDung = _insert.MaNguoiDung;
            like.ThoiGianLike = _insert.ThoiGianLike;
            like.Deleted = _insert.Deleted;            

            dbRaoVat.LIKE_TINRAOVATs.InsertOnSubmit(like);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maLike)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LIKE_TINRAOVAT _delete = (from n in dbRaoVat.LIKE_TINRAOVATs
                                 where n.MaLike == maLike && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(Like_TinRaoVatDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LIKE_TINRAOVAT _update = (from n in dbRaoVat.LIKE_TINRAOVATs
                                      where n.MaLike == dto.MaLike && n.Deleted == 0
                                      select n).Single();

            _update.MaTinRaoVat = dto.MaTinRaoVat;
            _update.MaNguoiDung = dto.MaNguoiDung;
            _update.ThoiGianLike = dto.ThoiGianLike;
            
            dbRaoVat.SubmitChanges();
        }
    }
}
